package cn.net.mysoft.account.api.common;


import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class AlgInNeed {

   public  static final String TOKEN =  "wapuboy20170421" ;

   public static String genEmailConfirmingString(int uid,long tm,String email){
       String inStr = email + uid + TOKEN + tm;

       return  shaEncode(inStr);
   }

    public static boolean isValidEmailConfirmingSign(int uid,long tm,String email,String sign,long expire){
        if(tm+expire < getUnixTimeStamp()) return false;

        String inStr = email + uid + TOKEN + tm;

        String shouldBeSign =   shaEncode(inStr);

        return shouldBeSign.equalsIgnoreCase(sign);

    }

   public static boolean checkPasswd(String seed,String stxt,String passwd){
       return shaEncode(passwd+seed).equalsIgnoreCase(stxt);
   }

   public static String getDictationSessionKey(String uin){
       return shaEncode(uin+TOKEN);
   }

   public static boolean isValidDictationSessionKey(String uin,String key){
       return  key.equalsIgnoreCase(getDictationSessionKey(uin));
   }


    public static String shaEncode(String inStr) {
        MessageDigest sha;
        try {
            sha = MessageDigest.getInstance("SHA");


            byte[] byteArray = inStr.getBytes(StandardCharsets.UTF_8);

            byte[] md5Bytes = sha.digest(byteArray);

            StringBuilder hexValue = new StringBuilder();

            for (byte md5Byte : md5Bytes) {

                int val = ((int) md5Byte) & 0xff;

                if (val < 16) {
                    hexValue.append("0");
                }
                hexValue.append(Integer.toHexString(val));
            }
            return hexValue.toString();

        } catch (Exception e) {

            System.out.println(e.toString());
            e.printStackTrace();
            return "";
        }

    }


    public static String TimeStamp2Date() {
        String formats = "yyyy-MM-dd HH:mm:ss";

        return new SimpleDateFormat(formats, Locale.CHINA).format(new Date());
    }

    public static String TimeStamp2Date(String timestampString) {
        String formats = "yyyy-MM-dd HH:mm:ss";
        long timestamp = Long.parseLong(timestampString) * 1000;
        return new SimpleDateFormat(formats, Locale.CHINA).format(new Date(timestamp));
    }

    public static Long getHalfAnHourTimeStamp(){
       Date now = new Date();
       return (now.getTime()/1000/10);
    }
    public static Long getUnixTimeStamp(){
        Date now = new Date();
        return (now.getTime()/1000);
    }


    private static double rad(double d)
    {
        return d * Math.PI / 180.0;
    }

    public static double GetDistance(double lat1, double lng1, double lat2, double lng2)
    {
        double radLat1 = rad(lat1);
        double radLat2 = rad(lat2);
        double a = radLat1 - radLat2;
        double b = rad(lng1) - rad(lng2);
        double s = 2 * Math.asin(Math.sqrt(Math.abs(Math.pow(Math.sin(a/2),2) +
                Math.cos(radLat1)*Math.cos(radLat2)*Math.pow(Math.sin(b/2),2))));
        //private static double EARTH_RADIUS = 6378.137;
        double EARTH_RADIUS = 6371.393;
        s = s * EARTH_RADIUS;
        s = Math.round(s * 1000);
        return s;
    }

}
